package cn.com.customer.manager.ui.message.notice

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.com.customer.manager.Constants
import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.MessageListAdapter
import cn.com.customer.manager.base.BaseFragment
import cn.com.customer.manager.bean.MessageListInfo
import cn.com.customer.manager.bean.Notice
import com.chad.library.adapter.base.BaseQuickAdapter
import kotlinx.android.synthetic.main.fragment_notice.*


/**
 * ============================================
 * 文件名：AnswerFragment.kt
 * 作者：lenovo
 * 日期：2018/1/19 14:42
 * 更新：2018/1/19 14:42
 * ============================================
 */
class NoticeFragment : BaseFragment(), BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, NoticeContract.View {
    private lateinit var mAdapter: MessageListAdapter
    private var mPresenter: NoticeContract.Presenter = NoticePresenter(this)
    private var page: Int = 1
    private var type: Int = 0
    private var tabTye = 0
    private var requestParam: MutableMap<String, String> = mutableMapOf()
    private var list: MutableList<Notice> = mutableListOf()
    private lateinit var result: MessageListInfo
    override fun getLayoutId(): Int {
        return R.layout.fragment_notice
    }

    override fun initView() {
        initRv()
        requestData()
        notice_tab.setDefaultSelection(0)
        notice_tab.setOnTabSelectionChangedListener { title, _ ->
            tabTye = if (title == "已读") {
                1
            } else {
                0
            }
            page = 1
            requestData()
        }
    }

    private fun initRv() {
        baseRecycler.layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
        mAdapter = MessageListAdapter(R.layout.item_message_list)
        baseRecycler.adapter = mAdapter
        mAdapter.onItemClickListener = this
        mAdapter.onItemChildClickListener = this
        mAdapter.setOnLoadMoreListener(this, baseRecycler)
    }

    override fun onLoadMoreRequested() {
        page++
        requestData()
    }

    private fun requestData() {
        requestParam.clear()
        requestParam["pageNo"] = page.toString()
        requestParam["pageSize"] = Constants.PAGE_SIZE.toString()
        requestParam["noticeTitle"] = ""
        mPresenter.getNoticeData(requestParam)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

    }

    companion object {

        @JvmStatic
        fun newInstance(goodsId: Int) = NoticeFragment().apply {
            arguments = Bundle().apply {
                putInt(Constants.EXTRA_TYPE, goodsId)
            }
        }
    }

    override fun setNoticeDate(result: MessageListInfo) {

        arguments?.let {
            type = it.getInt(Constants.EXTRA_TYPE, type)
        }
        list = if (type == 0) {
            if (tabTye == 0) {
                result.orgNotice.notReadList as MutableList<Notice>
            } else {
                result.orgNotice.readList as MutableList<Notice>
            }
        } else {
            if (tabTye == 0) {
                result.sysNotice.notReadList as MutableList<Notice>
            } else {
                result.sysNotice.readList as MutableList<Notice>
            }
        }
        if (page == 1) {
            mAdapter.setNewData(list)
        } else {
            mAdapter.addData(list)
        }
        if (mAdapter.isLoadMoreEnable and mAdapter.isLoading) {
            mAdapter.loadMoreComplete()
        }
    }


    override fun setPresenter(presenter: NoticeContract.Presenter) {
        mPresenter = presenter
    }

}
